package com.study.www.config;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author : 89007337.zhuyanpeng
 * @date :Create in  2019/8/23- 15:38
 **/
@Configuration
public class MapperCannerConfigurer {
    //包地址 test1
    private static final String TEST1BACKPACKAGE = "com.study.www.test1.dao";
    //包地址 test2
    private final static String TEST2BASEPACKAGES = "com.study.www.test2.dao";
    public final static String TEST1TRANSACTIONMANAGER = "test1TransactionManager";
    public final static String TEST2TRANSACTIONMANAGER = "test2TransactionManager";
    /**
     * 指定包路径和 SqlSessionFactory 工厂 的对应关系  test1
     */
    @Bean
    @Primary
    MapperScannerConfigurer test1MapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(TEST1BACKPACKAGE);
        mapperScannerConfigurer.setSqlSessionFactoryBeanName(Test1DataSourceConfig.TEST1SQLSESSIONFACTORY);
        return mapperScannerConfigurer;
    }

    /**
     * 指定包路径和 SqlSessionFactory 工厂 的对应关系  test2
     */
    @Bean
    MapperScannerConfigurer test2MapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(TEST2BASEPACKAGES);
        mapperScannerConfigurer.setSqlSessionFactoryBeanName(Test2DataSourceConfig.TEST2SQLSESSIONFACTORY);
        return mapperScannerConfigurer;
    }

    /*************************** Spring事务的配置 此处不可使用因为是多源 *********************/

    /**
     * 数据源1的事务
     */

    @Bean(TEST1TRANSACTIONMANAGER)
    @Primary
    PlatformTransactionManager test1TransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     *   数据源2的事务
     */
    @Bean(TEST2TRANSACTIONMANAGER)
    PlatformTransactionManager test2TransactionManager(@Qualifier("test2DataSource")DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }


    /*************************** sqlSessionTemplate 配置 非必须用则需要不用则可以跳过 *********************/
    /*
    @Bean
    @Primary
    public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier(Test1DataSourceConfig.TEST1SQLSESSIONFACTORY)SqlSessionFactory sessionFactory){
        return new SqlSessionTemplate(sessionFactory);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier(Test2DataSourceConfig.TEST2SQLSESSIONFACTORY)SqlSessionFactory sessionFactory){
        return new SqlSessionTemplate(sessionFactory);
    }*/

}
